package com.yhn.registration.controller;

import com.hospital.common.base.Result;
import com.hospital.common.pojo.TDepartment;
import com.hospital.common.pojo.TDoctorInfo;
import com.hospital.common.pojo.TDoctorSchedule;
import com.yhn.registration.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 科室接口
 */
@RestController
@RequestMapping("/api/departments")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;


    /**
     * 获取所有科室信息
     */
    @GetMapping("/list")
    public Result<List<TDepartment>> list() {
        return Result.success(departmentService.listAll());
    }


    /**
     * 医生查询接口
     * @return
     */
    @GetMapping("/doctors")
    public Result<List<TDoctorInfo>> listDoctors() {
        return Result.success(departmentService.doctor());
    }



    /**
     * 按科室ID查询该科室所有医生
     * @param deptId 科室ID
     */
    @GetMapping("/{deptId}/doctors")
    public Result<List<TDoctorInfo>> listDoctorsByDept(@PathVariable("deptId") Long deptId) {
        return Result.success(departmentService.doctorByDept(deptId));
    }

    /**
     * 根据医生姓名查询医生信息
     * @param name
     * @return
     */
    @GetMapping("/doctors/by-name/{name}")
    public Result<List<TDoctorInfo>> getDoctorByName(@PathVariable("name") String name) {
        return Result.success(departmentService.getDoctorByName(name));
    }

    /**
     * 根据医生ID查询排班信息
     * @param doctorId
     * @return
     */
    @GetMapping("/doctors/{doctorId}/schedule")
    public Result<List<TDoctorSchedule>> getDoctorSchedule(@PathVariable("doctorId") Long doctorId) {
        List<TDoctorSchedule> schedules = departmentService.getDoctorSchedule(doctorId);
        return Result.success(schedules);
    }

    /**
     * 按科室与排班日期筛选医生（可选hourSlot）
     * @param deptId
     * @param date
     * @param hourSlot
     * @return
     */
    @GetMapping("/{deptId}/doctors/by-schedule")
    public Result<List<TDoctorInfo>> listDoctorsByDeptAndSchedule(
            @PathVariable("deptId") Long deptId,
            @RequestParam("date") String date,
            @RequestParam(value = "hourSlot", required = false) Integer hourSlot) {
        return Result.success(departmentService.doctorsByDeptAndSchedule(deptId, date, hourSlot));
    }


    /**
     * 获取医生姓名和用户ID
     * @param infoId
     * @return
     */
    @GetMapping("/doctors/name/{infoId}")
    public Result<Map<String, Object>> getDoctorNameByInfoId(@PathVariable("infoId") Long infoId) {
        String name = departmentService.getDoctorNameByInfoId(infoId);
        Long userId = departmentService.getUserIdByInfoId(infoId);
        Map<String, Object> result = new HashMap<>();
        result.put("name", name);
        result.put("userId", userId);
        return Result.success(result);
    }
}


